home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Overload Trio 2
/
Shareware Overload Trio Volume 2 (Chestnut CD-ROM).ISO
/
dir30
/
alsps.zip
/
ALSP3.DOC
< prev
next >
Wrap
Lisp/Scheme
|
1993-11-04
|
8KB
|
314 lines
This is Lesson 3 of a series of AutoLISP training exercises given
on the CompuServe ADESK Forum by the Autodesk, Inc. Training
Department.
DATA TYPES
There are two kinds of objects in AutoLISP. Atoms are simple
objects; lists are complex objects bounded by parentheses. Among
simple objects, there are many sub-categories known as data types.
A clear understanding of data types in AutoLISP is important.
Along with their parent classes of objects, atoms and lists,
they form the essential building blocks for all AutoLISP
programs.
Data types generally describe the nature of the data that the
atom is to hold, and the amount of space (or memory) AutoLISP
must allocate to create the atom and store its value. This
lesson will concentrate on the former topic; memory management
will be discussed in a future lesson(s).
This is the list of data types supported by Release 10. All
except one are sub-categories of atoms; the exception is the data
type "list". Lists themselves qualify as a distinct data type.
* list
* symbols
* strings
* real numbers
* integers
* file descriptors
* AutoCAD entity "names"
* AutoCAD selection-sets
* subrs (built-in functions)
DETERMINING DATA TYPES
The AutoLISP function (type) may be used to determine the data
type of any object. (type) takes one required argument, and
returns a symbol that indicates the data type of its argument.
For example, this form (a "form" being any list that can be
evaluated) determines the data type of the integer 1.
Command: (type 1)
INT
EXAMPLES OF DATA TYPES
What follows is example AutoLISP code that binds variable X to a
variety of objects with different data types and determines the
data type of its binding with the (type) function. You may want
to type in the expressions at the command prompt in AutoCAD.
In cases where a hexadecimal number appears as a part of the data
type, no effort has been made to accurately represent the number,
as it varies from time to time. Instead, the number is
represented in the examples simply as "xxxx".
LIST
Command: (setq x (quote (1 2 3)))
(1 2 3)
Command: !x
(1 2 3)
Command: (type x)
LIST
SYMBOLS
Command: (setq x 'A)
A
Command: !x
A
Command: (type a)
SYM
STRINGS
Text strings can be of any length, up to the maximum amount of
memory that can be allocated to them. This value varies
depending on the amount of AutoLISP code and data currently in
use, and the type of AutoLISP in use (Regular or Extended).
String constants are limited to a maximum length of 100 characters.
Command: (setq x "Text string")
"Text string"
Command: !x
"Text string"
Command: (type x)
STR
REAL NUMBERS
AutoLISP represents real numbers as double precision floating
point numbers, accurate to at least 14 significant digits of
precision. There is no data type to represent single precision
floating point numbers in AutoLISP; all reals are double
precision, and are represented in AutoLISP in the same manner
that they are represented in AutoCAD.
DISPLAY ACCURACY AND INTERNAL ACCURACY
The display accuracy of real numbers in AutoLISP is only that;
the display accuracy. The internal representation is always
accurate to at least 14 digits of precision. If it is necessary
to display or to print the value of a real number to some
arbitrary precision beyond AutoLISP's default display accuracy,
the real number is typically converted to a string with the
AutoLISP function (rtos) and displayed or printed at the desired
display accuracy. (rtos) will be discussed in a future lesson on
data type conversion.
Command: (setq x 4.5)
4.5
Command: !x
4.5
Command: (type x)
REAL
INTEGERS
In DOS AutoCAD, integers are 16-bit signed numbers between -32768
and +32767. In all other versions of AutoCAD, they are 32-bit
signed numbers between -2,147,483,648 and +2,147,483,647.
Integers transferred between AutoLISP and AutoCAD are restricted
to 16-bit values. For compatibility reasons across versions of
AutoCAD, it is generally best to use 16-bit values only in
AutoLISP programs.
Command: (setq x 1)
1
Command: !x
1
Command: (type x)
INT
FILE DESCRIPTORS
A file descriptor is an internal pointer to a file that has been
opened for a read/write/append operation. Functions desiring to
act on an open file must use its file descriptor to access the
file; therefore, when a file is opened, a variable must be bound
to the file descriptor or the file cannot be accessed again.
In this example, the (open) function is use to open a file named
"file.ext" in the default directory and disk drive for a write
operation. The (open) function returns the file descriptor for
"file.ext".
Command: (setq x (open "file.ext" "w"))
<File: xxxx>
Command: !x
<File: xxxx>
Command: (type x)
FILE
Command: (setq x (close x))
nil
AUTOCAD ENTITY "NAMES"
An entity name is a pointer to an entity's record in the current
drawing editor session. Entity names change from session to
session, and the same name is valid only in the editing session
where it was retrieved (but it will be valid for that entire
session).
There is no way to store an entity name within an AutoCAD drawing
or an external file for subsequent use in a different drawing
editor session; this facility has been deliberately disallowed.
Entity names allow AutoLISP programs to act on unique, individual
entities. Methods to accomplish this will be explored in future
lessons; a brief example is illustrated here.
The (entlast) function used in this example returns the entity
name of the last entity added to the drawing.
Command: LINE
From point: 1,1
To point: 5,5
To point: return
Command: (setq x (entlast))
<Entity name: xxxx>
Command: !x
<Entity name: xxxx>
Command: (type x)
ENAME
Command: ERASE
Select objects: !x
Select objects: return
AUTOCAD SELECTION-SETS
A selection-set is a collection of entity names, stored in a
temporary file. No more than six selection-sets may be opened by
AutoLISP at one time. To close an unneeded selection-set
variable, bind the variable to nil.
The (ssget) function used in this example prompts the user to
create a selection-set and returns the selection-set as the value
of the expression.
Command: LINE
From point: 1,1
To point: 5,5
To point: 10,1
To point: return
Command: (setq x (ssget))
Select objects: pick first line
Select objects: pick second line
Select objects: return
<Selection set: xxxx>
Command: !x
<Selection set: xxxx>
Command: (type x)
PICKSET
Command: ERASE
Select objects: !x
Select objects: return
SUBRS
A subr is a built-in AutoLISP function. See the Table of
Contents for Chapters 4 and 5 of the AutoLISP Programmer's
Reference for a list of most of the subrs in AutoLISP.
Command: !setq
<Subr: xxxx>
Command: (type setq)
SUBR
QUESTIONS
1. What is the internal accuracy of real numbers in AutoLISP?
2. Does AutoLISP have a data type for a single precision
floating point number?
3. Why should a program use only 16-bit integers rather than
32-bit integers in non-DOS versions of AutoCAD?
4. What happens when a file is opened but its file descriptor
isn't assigned to a variable?
5. How are selection-sets stored in AutoLISP?
6. How many selection-sets can be opened at one time?
7. What is an entity name?
8. How can the same entity name be stored and used in different
drawing editor sessions?
9. What are the maximum lengths for a string constant and a
string variable?
10. What function can be used to determine the data type of any
object or variable?
Next week: AutoLISP and the AutoCAD Calculator
Answers to Lesson 2 Exercises:
1. legal
2. legal
3. face value
4. legal
5. legal
6. face value
1. (+ 3 10 5)
2. (* 20 15)
3. (- 16 10)
4. (/ 15 3)
5. (+ 5 (* 10 2))
6. (* (+ 5 10) 2)
1. (setq x 1)
!x
2. (setq x 4.5)
!x
3. (setq x "text")
!x
4. (setq x (quote (1.0 2.0 3.0)))
!x